<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class OrderMember extends Api
{
    protected $with = ['order_fleet.order_member','user_index','business','virtual_characters'];
    //商家端-添加预约人物【虚拟人物】
    public function add()
    {
        $this->businessCheckLogin();
        $post = input('post.');
        if(!isset($post['num']) || !isset($post['id'])) return msgerror('参数缺失');
        $OrderFleet_model = app('app\api\model\OrderFleet');
        $OrderFleet_data = $OrderFleet_model->where('id',$post['id'])->with('order_member')->find();
        if(empty($OrderFleet_data) || (!empty($OrderFleet_data) && empty($OrderFleet_data->toArray()))) return msgerror('车队不存在');
        $OrderFleet_data = $OrderFleet_data->toArray();
        if($OrderFleet_data['business_id'] != $this->businessinfo['id']) return msgerror('该车队不是该门店所属，无法添加虚拟人物');
        if($post['num'] <= 0) return  msgerror('添加失败，数量不能小于等于0');
        $sy = ($OrderFleet_data['script_f_num']*1+$OrderFleet_data['script_m_num']*1)-count($OrderFleet_data['order_member']);
        //虚拟人物数量必须要比剩余总数量小1
        if($post['num'] > ($sy-1)) return msgerror('添加失败，添加人数大于车队剩余可添加人数');
        $VirtualCharacters_model = app('app\api\model\VirtualCharacters');
        $VirtualCharacters_data = $VirtualCharacters_model->select();
        if(!empty($VirtualCharacters_data) && !empty($VirtualCharacters_data->toArray()))
        {
            $VirtualCharacters_data = $VirtualCharacters_data->toArray();
            if($post['num'] > count($VirtualCharacters_data)) return msgerror('添加失败，添加数量大于虚拟人物库现存人数，请联系平台添加完善虚拟人物库');
            $id = [];
            foreach ($VirtualCharacters_data as $v){
                $id[] = $v['id'];
            }
            //rand_arr_from_array 自定义方法随机从数组中获得指定数量的元素
            $member_data = rand_arr_from_array($id, $post['num']);
            $data = [];
            //组装人员数据，批量添加
            foreach($member_data as $v)
            {
                $VirtualCharacters_data = $VirtualCharacters_model->where('id',$v)->find()->toArray();
                $data[] = ['order_fleet_id'=>$post['id'],'virtual_characters_id'=>$v,'user_type'=>3,'name'=>$VirtualCharacters_data['name']];
            }
            $this->model->saveAll($data);
            return datasuccess([],'添加成功');
        }else{
            return  msgerror('虚拟人物库还未添加数据，无法生成数据');
        }
    }
    //商家端-移除车队成员
    public function bus_delete()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!isset($post['id'])) return msgerror('参数缺失');
        $data = $this->model->with($this->with)->where('id',$post['id'])->find();
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
            if($data['order_fleet']['business_id'] != $this->businessinfo['id'])
            {
                return msgerror('移除失败，该成员不是该门店所属');
            }
            $this->model::destroy($post['id']);
            $member = $data['order_fleet']['order_member'];
            $msg_data = [];
            foreach ($member as $k=>$v)
            {
                if($v['user_type'] == 1){
                    $msg_data[] = ['type'=>1,'name'=>'车队人员减少','user_index_id'=>$v['user_index_id'],'content'=>'一位车队成员以被踢出'];
                }elseif($v['user_type'] == 2){
                    $msg_data[] = ['type'=>2,'name'=>'车队人员减少','business_id'=>$v['user_index_id'],'content'=>'一位车队成员以被踢出'];
                }
            }
            $Msg_model = app('app\api\model\Msg');
            $Msg_model->saveAll($msg_data);
        }else{
            return msgerror('该成员已被移除车队');
        }
        return datasuccess();
    }
    //用户端-个人中心-我玩过的
    public function wg()
    {
        $this->checkLogin();
        $post = param();
        $limit = $post['limit'] ?? 20;
        $data = $this->model->where([
            ['user_index_id','=',$this->userinfo['id']],
            ['status','=',1],
            ['is_del','=',2],
            ['wc','=',1]
        ])->with('order_fleet.script')->group('order_fleet_id')->paginate($limit);
        return datasuccess($data);
    }
    //用户端-个人中心-我的预约-我组建的
    public function zj()
    {
        $this->checkLogin();
        $post = param();
        $limit = $post['limit'] ?? 20;
        $order = $post['order'] ?? 'id desc';
        $data = $this->model->where([
            ['user_index_id','=',$this->userinfo['id']],
            ['cj_status','=',1],
            ['is_del','=',2]
        ])->with(['order_fleet'=>['script','business','order_member'=>['user_index', 'business', 'virtual_characters']]])->order($order)->group('order_fleet_id')->paginate($limit);
        return datasuccess($data);
    }
    //用户端-个人中心-我的预约-我参与的
    public function cy()
    {
        $this->checkLogin();
        $post = param();
        $limit = $post['limit'] ?? 20;
        $order = $post['order'] ?? 'id desc';
        $data = $this->model->where([
            ['user_index_id','=',$this->userinfo['id']],
            ['is_del','=',2]
        ])->with(['order_fleet'=>['script','business','order_member'=>['user_index', 'business', 'virtual_characters']]])->order($order)->group('order_fleet_id')->paginate($limit);
        return datasuccess($data);
    }
}